<template>
  <!-- 头部区域 -->
  <div class="home-content">
    <!-- 搜索组件 -->
    <mysearch></mysearch>

    <!-- 分类区域 -->
    <div class="home-classify">
      <ul>
        <li v-for="(item, i) in classify.slice(0, 10)" :key="i" @click="goodscategory(item)">
          <img :src="item.image" alt="" />
          <span>{{ item.title }}</span>
        </li>
      </ul>
    </div>
    <!-- 商品列表区域 -->
    <div class="home-list">
      <div class="home-list-wrp">
        <ul>
          <li v-for="(goods, i) in goods1" :key="i" @click="gotoVideodetail(goods)">

            <div class="myimg">
              <img :src="goods.itemImage" alt="" />
              <img src="../assets/image/home/play.png" alt="" class="play-icon" />
              <!-- 头像区域 -->
              <div class="userName">
                <div class="userImg">
                  <img :src="goods.actorAvatar" alt="" />
                </div>
                <p class="actorName">{{ goods.actorName }}</p>
                <div class="userName-right">
                  <p class="actorTag">{{ goods.actorTag }}</p>
                </div>
              </div>
            </div>
            <div class="goods-info">
              <p>{{ goods.title }}</p>
              <div class="goods-price-wrp">
                <div class="goods-price">
                  <span class="price-icon">¥</span>
                  <span>{{ goods.showOrgPrice.split(".")[0] }}.</span>
                  <span class="price-fen">{{
                    goods.showOrgPrice.split(".")[1]
                    }}</span>
                </div>
                <img :src="goods.bottomIcon" alt="" class="bottomIcon" />
                <div class="sale">
                  <span>{{ goods.sale }}</span>
                </div>
              </div>
            </div>
          </li>
        </ul>

        <ul>
          <li v-for="(goods, i) in goods2" :key="i" @click="gotoVideodetail(goods)">

            <div class="myimg">
              <img :src="goods.itemImage" alt="" />
              <img src="../assets/image/home/play.png" alt="" class="play-icon" />
              <!-- 头像区域 -->
              <div class="userName">
                <div class="userImg">
                  <img :src="goods.actorAvatar" alt="" />
                </div>
                <p class="actorName">{{ goods.actorName }}</p>
                <div class="userName-right">
                  <p class="actorTag">{{ goods.actorTag }}</p>
                </div>
              </div>
            </div>

            <div class="goods-info">
              <p>{{ goods.title }}</p>
              <div class="goods-price-wrp">
                <div class="goods-price">
                  <span class="price-icon">¥</span>
                  <span>{{ goods.showOrgPrice.split(".")[0] }}.</span>
                  <span class="price-fen">{{
                    goods.showOrgPrice.split(".")[1]
                    }}</span>
                </div>
                <img :src="goods.bottomIcon" alt="" class="bottomIcon" />
                <div class="sale">
                  <span>{{ goods.sale }}</span>
                </div>
              </div>
            </div>
          </li>
        </ul>
      </div>
      <!-- 加载load -->
      <div class="load" ref="loadmore">正在加载中...</div>
    </div>
  </div>
</template>

<script>
import mysearch from "../components//My-search";
export default {
  components: {
    mysearch,
  },
  data() {
    return {
      classify: [], //分类数据
      goodsList: [], //商品列表
      page: 1, //页数
      isloading: false, //是否在发请求
      goods1: [],
      goods2: [],
    };
  },

  computed: {},
  mounted() {
    window.addEventListener("scroll", this.scrollEvents);
  },
  beforeDestroy() {
    window.removeEventListener("scroll", this.scrollEvents);
  },
  methods: {
    // 获取分类数据
    async getClassify() {
      const { data: res } = await this.axios.get("/homeNav.json");
      this.classify = res;
    },
    // 跳转goodscategor页
    goodscategory(item) {
      if (item.title == "新人福利") return;
      this.$router.push({
        path: "/goodscategory",
        query: {
          keyword: item.title,
        },
      });
    },
    // 跳转直播回放页
    gotoVideodetail(goods){
    this.$router.push({
      path:'/videodetail',
      query:{
        actorIdUrl: goods.actorIdUrl,
        itemIdUrl: goods.itemIdUrl
      }
    })
    },
  
    // 获取商品列表
    async getGoodsList() {
      // 打开节流阀
      this.isloading = true;
      const { data: res } = await this.axios.get(
        "/video/item?page=" + this.page
      );
      // 关闭节流阀
      this.isloading = false;
      this.goodsList = res.data.list;
      console.log(this.goodsList);

      this.goodsList.forEach((element, index) => {
        if (index < this.goodsList.length / 2) {
          this.goods1.push(element);
          this.goods1 = Array.from(new Set(this.goods1));
        }
        if (index >= this.goodsList.length / 2) {
          this.goods2.push(element);
          this.goods2 = Array.from(new Set(this.goods2));
        }
      });
    },
    // 触底加载
    scrollEvents() {
      let scrollTop =
        document.documentElement.scrollTop || document.body.scrollTop;
      let h = this.$refs.loadmore.offsetTop;
      //获窗口的内容可视区高度
      let clientHeight = window.innerHeight;
      if (scrollTop + clientHeight >= h + 30) {
        if (this.isloading) return;
        this.page++;
        this.getGoodsList();
      }
    },
  },
  created() {
    this.getClassify();
    this.getGoodsList();
  },
};
</script>

<style lang="scss" scoped>
// 加载load样式
.load {
  height: 50px;
  line-height: 50px;
  text-align: center;
  font-size: 14px;
  color: #666;
}
.home-content {
  margin-bottom: 50px;
  margin-top: 50px;
}

// 分类区域样式
.home-classify {
  ul {
    display: flex;
    flex-wrap: wrap;

    li {
      width: 20%;
      display: flex;
      align-items: center;
      flex-direction: column;
      font-size: 12px;
      color: #666666;
      margin-top: 10px;
      span {
        margin-top: 5px;
      }
    }
  }
  img {
    width: 50px;
    height: 50px;
  }
}
.home-list {
  width: 100%;
  height: 100%;
  background-color: #f6f6f6;
  margin-top: 10px;
  .home-list-wrp {
    padding: 12px 7px;
    display: flex;

    ul {
      margin-left: 5px;
      margin-right: 5px;
      width: 47%;
      li {
        position: relative;
    
        img {
          width: 100%;
          height: 100%;
          object-fit: cover;
          display: block;
        }
        .myimg {
          position: relative;
          .userName {
            position: absolute;
            font-size: 12px;
            color: #fff;
            bottom: 0;
            width: 100%;
            height: 40px;
            display: flex;
            align-items: center;
            box-sizing: border-box;
            white-space: nowrap;
            .actorName {
              margin-left: 5px;
            }

            .userImg {
              width: 17px;
              height: 17px;
              border: 1px solid #fff;
              border-radius: 50%;
              margin-left: 9px;
              img {
                width: 100%;
                border-radius: 50%;
              }
            }
            .userName-right {
              margin-left: 5px;
              background-color: rgba(0, 0, 0, 0.3);
              .actorTag {
                line-height: 20px;
                padding: 0 3px;
              }
            }
          }
        }
        .play-icon {
          width: 28px;
          height: 28px;
          position: absolute;
          top: 5px;
          right: 5px;
        }
        .goods-info {
          width: 100%;
          padding: 9px;
          background-color: #fff;
          box-sizing: border-box;
          p {
            font-size: 12px;
            line-height: 16px;
            margin-bottom: 10px;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
          }
          .goods-price-wrp {
            display: flex;
            align-items: center;
            .price-icon {
              margin-right: 2px;
            }
            .goods-price {
              color: #f46;
              span {
                font-size: 16px;
              }
              .price-fen,
              .price-icon {
                font-size: 12px;
              }
            }
            .bottomIcon {
              width: 22px;
              height: 12px;
              margin-left: 3px;
            }
            .sale {
              flex: 1;
              text-align: right;
              font-size: 12px;
              color: #f46;
              span {
                background-color: #ffecef;
                padding: 2px;
                margin-left: 5px;
              }
            }
          }
        }
      }
    }
  }
}
</style>